﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseData;
using BaseData.UI;
using TaxBiz.SysConfig;
using TaxBiz.Tax;
using TaxEntity.Tax;
using TaxEntity.House;

namespace Admin.Tax
{
    public partial class TaxModify : BaseCommon.BasePage
    {
        protected string id = string.Empty;
        protected int year = DateTime.Now.Year;
        protected TaxCompleteBiz tb = new TaxCompleteBiz();
        protected TaxCompleteEntity te = new TaxCompleteEntity();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HasPower())
            {
                Jscript.AlertAndGoBack("您权限不足，不能操作");
                return;
            }
            id = PageContext.GetRequest("id");
            if (string.IsNullOrEmpty(id))
            {
                Jscript.AlertAndGoBack("参数错误");
                return;
            }
            long iId = 0;
            long.TryParse(id, out iId);
            if (iId <= 0)
            {
                Jscript.AlertAndGoBack("参数错误");
                return;
            }
            te = tb.GetEntityById(iId);
            if (te == null)
            {
                Jscript.AlertAndGoBack("参数错误");
                return;
            }
            string action = PageContext.GetRequest("action");
            if (!string.IsNullOrEmpty(action))
            {
                this.DoAction(action, iId);
            }
        }

        protected bool DoAction(string action, long id)
        {
            te.ID = id;
            te.OptUserID = UserLogin.UserId;
            te.ModifyTime = DateTime.Now;
            te.Year = PageContext.GetRequestInt("selYear").Value;
            //te.CertificateID = PageContext.GetRequest("txtCertificateID");
            string strTempDate = PageContext.GetRequest("txtStartDate");
            if (!string.IsNullOrEmpty(strTempDate))
            {
                te.StartDate = DateTime.Parse(strTempDate);
            }
            strTempDate = PageContext.GetRequest("txtEndDate");
            if (!string.IsNullOrEmpty(strTempDate))
            {
                te.EndDate = DateTime.Parse(strTempDate);
            }
            te.IsTaxOK = PageContext.GetRequestInt("rdIsTaxOK").Value;
            strTempDate = PageContext.GetRequest("txtComDate");
            if (!string.IsNullOrEmpty(strTempDate))
            {
                te.ComDate = DateTime.Parse(strTempDate);
            }
            //te.RentID = PageContext.GetRequestLong("txtRentID").Value;
            te.TaxInputType = PageContext.GetRequest("selType");
            te.TotalTax = PageContext.GetRequestDecimal("txtTotalTax");
            te.FangchanTax = PageContext.GetRequestDecimal("txtFangchanTax");
            te.TudiTax = PageContext.GetRequestDecimal("txtTudiTax");
            te.YingyeTax = PageContext.GetRequestDecimal("txtYingyeTax");
            te.PersonalTax = PageContext.GetRequestDecimal("txtPersonalTax");
            te.CityTax = PageContext.GetRequestDecimal("txtCityTax");
            te.JiaoyuTax = PageContext.GetRequestDecimal("txtJiaoyuTax");
            te.DifangJiaoyuTax = PageContext.GetRequestDecimal("txtDifangJiaoyuTax");
            te.YinhuaTax = PageContext.GetRequestDecimal("txtYinhuaTax");
            ComputeTotalTax(ref te);

            bool isSucc = tb.Update(te, UserLogin);
            if (!isSucc)
            {
                Jscript.Alert("修改失败");
                return false;
            }

            Jscript.AlertAndRedirect("修改成功", "TaxModify.aspx?id=" + id);
            return true;
        }

        protected void ComputeTotalTax(ref TaxCompleteEntity tce)
        {
            if (tce != null)
            {
                //分税种录入 重新计算总税额
                if (tce.TaxInputType == "分税种录入")
                {
                    tce.TotalTax = GetDecimalValue(tce.FangchanTax) + GetDecimalValue(tce.TudiTax) +
                        GetDecimalValue(tce.YingyeTax) + GetDecimalValue(tce.PersonalTax) +
                        GetDecimalValue(tce.CityTax) + GetDecimalValue(tce.JiaoyuTax) +
                        GetDecimalValue(tce.DifangJiaoyuTax) + GetDecimalValue(tce.YinhuaTax);
                }
            }
        }

        protected decimal GetDecimalValue(decimal? d)
        {
            return d.HasValue ? d.Value : 0;
        }
    }
}